//
//  HomeEnquiryView.swift
//  VastSwift
//
//  Created by ALH000355 on 2022/2/16.
//


import UIKit

// 点击view的回调
typealias ClickEnquiryViewSure = ((Int) -> Void)
// 我要询价view-包括我要询价、询价结果
class HomeEnquiryView: UIView {
    
    // 点击view回调事件
    var callback: ClickEnquiryViewSure!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.uiConfig()
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    func uiConfig() -> Void {
        let width = screenWidth - 30
        let backView = UIView.init(frame: CGRect.init(x: 15, y: 10, width: width, height: 64))
        backView.layer.cornerRadius = 5
        backView.layer.masksToBounds = true
        backView.layer.borderColor = colorTheme.cgColor
        backView.layer.borderWidth = 1
        self.addSubview(backView)
        
        let enquiryBtn = HomeEnquiryBtnView.init(frame: CGRect.init(x: 0, y: 0, width: width / 2, height: 64))
        backView.addSubview(enquiryBtn)
        enquiryBtn.backgroundColor = colorTheme
        enquiryBtn.showViewInfo(text: "我要询价", textColor: .white, detail: "一键发布、精准找货", detailColor: .white, logo: "icon_xunjia", numHide: true, num: 0) {
            if let callback1 = self.callback {
                callback1(1)
            }
        }
        
        let resultBtn = HomeEnquiryBtnView.init(frame: CGRect.init(x: width / 2, y: 0, width: width / 2, height: 64))
        backView.addSubview(resultBtn)
        resultBtn.backgroundColor = .white
        resultBtn.showViewInfo(text: "询价结果", textColor: .black, detail: "在线下单、售后保障", detailColor: .gray, logo: "icon_xunjiajieguo", numHide: true, num: 0) {
            if let callback1 = self.callback {
                callback1(2)
            }
        }
    }
}
// 最小的单元格,宽70，高90
class HomeEnquiryBtnView: UIView {
    
    // 展示的图片
    var showImageView: UIImageView!
    // 标题
    var titleLabel: UILabel!
    // 详细内容
    var detailLabel: UILabel!
    // 显示数字。默认不显示
    var numLabel: UILabel!
    // 点击view回调事件
    private var tap: ClickViewSure?
    
    func showViewInfo(text: String, textColor: UIColor = .gray, detail: String, detailColor: UIColor = .gray, logo: String, numHide: Bool = true, num: Int = 0, handle CompletionHandle: @escaping ClickViewSure) {
        titleLabel.text = text
        titleLabel.textColor = textColor
        
        detailLabel.text = detail
        detailLabel.textColor = detailColor
        
        showImageView.image = UIImage.init(named: logo)
        if numHide == false {
            
            numLabel.isHidden = numHide
            numLabel.text = "\(num)"
        }
        tap = CompletionHandle
    }
    @objc func clickView() {
        if let click = tap {
            click()
        }
    }
    
    func uiConfig() -> Void {
        self.backgroundColor = .clear
        showImageView = UIImageView.init(frame: CGRect.init(x: 12, y: 20, width: 24, height: 24))
        showImageView.backgroundColor = .clear
        showImageView.isUserInteractionEnabled = true
        
        titleLabel = UILabel.init(frame: CGRect.init(x: 48, y: 12, width: 70, height: 22))
        titleLabel.textAlignment = .left
        titleLabel.textColor = .white
        titleLabel.font = .systemFont(ofSize: 16)
        
        detailLabel = UILabel.init(frame: CGRect.init(x: 48, y: 38, width: 100, height: 14))
        detailLabel.textAlignment = .left
        detailLabel.textColor = .white
        detailLabel.font = .systemFont(ofSize: 10)
        self.addSubview(showImageView)
        self.addSubview(titleLabel)
        self.addSubview(detailLabel)
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(clickView))
        self.addGestureRecognizer(tap)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.uiConfig()
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
}
